package service

import (
	"encoding/json"
	"github.com/skip2/go-qrcode"
	"os"
	"path/filepath"
	"seat-service/initialization"
	"seat-service/model/dto"
)

type QrCodeService struct{}

func (q *QrCodeService) WriteQRCode(code []dto.WriteCodeParam) (err error) {
	// 获取可执行文件所在的目录 TODO
	executablePath, _ := os.Executable()
	executableDir := filepath.Dir(executablePath)
	path := filepath.Join(executableDir, "uploads", "code")
	for _, value := range code {
		//结构体序列化
		codeInformationJson, err := json.Marshal(&value)
		if err != nil {
			return err
		}
		fileName := initialization.GenUUID()
		//生成二维码
		qrCodeFilePath := filepath.Join(path, fileName+".png")
		err = qrcode.WriteFile(string(codeInformationJson), qrcode.Medium, 256, qrCodeFilePath)
		if err != nil {
			return err
		}
	}
	// 这里可以返回相应的成功信息或者nil，表明二维码生成成功
	return nil
}
